package com.yinhe.music.yhmusic.album.info;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.album.AlbumDetailActivity;
import com.yinhe.music.yhmusic.base.BaseBarStateChangeListener;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.comment.CommentListActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.login.LoginRegisterActivity;
import com.yinhe.music.yhmusic.model.AlbumInfo;
import com.yinhe.music.yhmusic.music.list.MusicListFragment;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ToastUtils;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.CommentView;

import butterknife.BindView;

/**
 * 专辑信息界面
 */
public class AlbumInfoActivity extends BaseServiceActivity implements View.OnClickListener, AlbumInfoContract.IAlbumInfoView {
    @BindView(R.id.album_art)
    ImageView albumArt;
    @BindView(R.id.menu_pic)
    ImageView menuPic;
    @BindView(R.id.pic_album)
    ImageView picAlbum;
    @BindView(R.id.menu_title)
    TextView menuTitle;
    @BindView(R.id.author_pic)
    ImageView authorPic;
    @BindView(R.id.author_name)
    TextView authorName;
    @BindView(R.id.arrow)
    ImageView arrow;
    @BindView(R.id.menu_collect)
    TextView menuCollect;
    @BindView(R.id.menu_down)
    TextView menuDown;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.collapsingToolbar)
    CollapsingToolbarLayout collapsingToolbar;
    @BindView(R.id.appbar)
    AppBarLayout appbar;
    @BindView(R.id.headerdetail)
    RelativeLayout headerDetail;
    @BindView(R.id.comment_view)
    CommentView commentIcon;
    @BindView(R.id.menu_comment)
    RelativeLayout menuComment;

    private int albumid;
    private AlbumInfo mAlbumInfo;
    private int isFollow;
    private ActionBar actionBar;
    int collectNum;

    private AlbumPresenter mPresenter;

    @Override
    public void initView() {
        hideToolbar();
        setTransparentToolbar("专辑");
        actionBar = getSupportActionBar();
        commentIcon.setColorString("#af316f");

        if (getIntent() != null) {
            albumid = getIntent().getIntExtra("albumId", 0);
            mPresenter.getAlbumInfo(albumid);
        }
        showQuickControl(true);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_menu_playlist;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AlbumPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void setMusicList() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction tx = fm.beginTransaction();
        MusicListFragment musicListFragment = MusicListFragment.newInstance(IConstants.ALBUM_MUSIC_LIST, albumid, null, mAlbumInfo.getSongsnum());
        tx.add(R.id.FrameLayout, musicListFragment);
        tx.commit();
    }

    private void setListener() {
        menuCollect.setOnClickListener(this);
        authorName.setOnClickListener(this);
        arrow.setOnClickListener(this);
        menuComment.setOnClickListener(this);
        menuDown.setOnClickListener(this);
    }

    private void initCollect() {
        Drawable top = getResources().getDrawable(isFollow == 0
                ? R.drawable.ic_favourite_white
                : R.drawable.ic_favourite_added);
        top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
        menuCollect.setCompoundDrawables(null, top, null, null);
    }

    private void collect() {
        if (!Preferences.getLoginSn().isEmpty()) {
            setCollectView();
        } else {
            startActivity(new Intent(AlbumInfoActivity.this,
                    LoginRegisterActivity.class));
        }
    }

    private void setCollectView() {
        mPresenter.collect(albumid, IConstants.ALBUM, isFollow);
    }

    private void initAlbumUI() {
        GlideHelper.setImageResource(menuPic, mAlbumInfo.getImage(), R.drawable.default_cover);
        GlideHelper.setBlurImageResource(albumArt, mAlbumInfo.getImage(), 25f, 0x99FFFFFF);

        authorPic.setVisibility(View.GONE);
        picAlbum.setVisibility(View.VISIBLE);
        arrow.setVisibility(View.VISIBLE);

        if (mAlbumInfo.getIntro() != null && !mAlbumInfo.getIntro().isEmpty()) {
            authorName.setText(String.format("简介： %s", mAlbumInfo.getIntro()));
        } else {
            authorName.setVisibility(View.GONE);
            arrow.setVisibility(View.GONE);
        }
        menuTitle.setText(mAlbumInfo.getAlbumName());

        appbar.addOnOffsetChangedListener(new BaseBarStateChangeListener() {
            @Override
            public void onOffSetChanged(AppBarLayout appBarLayout, int i) {
                actionBar.setBackgroundDrawable(null);
                float a = (float) Math.abs(i) / (appBarLayout.getTotalScrollRange());
                headerDetail.setAlpha(1f - a);
            }

            @Override
            public void onStateChanged(AppBarLayout appBarLayout, State state) {
                if (state == State.EXPANDED) {
                    setTitle(getResources().getString(R.string.album));
                } else if (state == State.COLLAPSED) {
                    setTitle(mAlbumInfo.getAlbumName());
                } else {
                    setTitle(getResources().getString(R.string.album));
                }
            }
        });

        commentIcon.setUnreadCount(mAlbumInfo.getCommentNum());
        isFollow = mAlbumInfo.getCollectStatus();

        initCollect();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_collect:
                collect();
                break;
            case R.id.author_name:
            case R.id.arrow:
                Intent intent = new Intent(this, AlbumDetailActivity.class);
                intent.putExtra("album", mAlbumInfo);
                startActivity(intent);
                break;
            case R.id.menu_comment:
                startCommentView();
                break;
            case R.id.menu_down:
                RxBus.get().post(RxBusEventType.Album.DOWNLOAD_ALBUM, RxbusNullObject.INSTANCE);
                break;
            default:
                break;
        }
    }

    private void startCommentView() {
        Intent it = new Intent(this, CommentListActivity.class);
        it.putExtra("name", mAlbumInfo.getAlbumName());
        it.putExtra("artist", mAlbumInfo.getSingerName());
        it.putExtra("cover", mAlbumInfo.getImage());
        it.putExtra("id", albumid);
        it.putExtra("commentType", IConstants.ALBUM);
        startActivity(it);
    }

    @Override
    public void setAlbumInfoUI(AlbumInfo albumInfo) {
        mAlbumInfo = albumInfo;
        collectNum = albumInfo.getCollectNum();
        menuCollect.setText(String.valueOf(collectNum));
        setListener();
        setMusicList();
        initAlbumUI();
    }

    @Override
    public void updateCollectUI() {
        if (isFollow == 0) {
            collectNum++;
            menuCollect.setText(String.valueOf(collectNum));
            UmengEventUtils.collect(this, UmengEventUtils.ALBUM);
            Toast.makeText(AlbumInfoActivity.this, R.string.album_collect_success_tip, Toast.LENGTH_SHORT).show();
            Drawable top = getResources().getDrawable(R.drawable.ic_favourite_added);
            top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
            menuCollect.setCompoundDrawables(null, top, null, null);
            isFollow = 1;
        } else {
            collectNum--;
            menuCollect.setText(String.valueOf(collectNum));
            Toast.makeText(AlbumInfoActivity.this, R.string.album_collect_fail_tip, Toast.LENGTH_SHORT).show();
            Drawable top = getResources().getDrawable(R.drawable.ic_favourite_white);
            top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
            menuCollect.setCompoundDrawables(null, top, null, null);
            isFollow = 0;
        }

        RxBus.get().post(RxBusEventType.Album.COLLECT_ALBUM, String.valueOf(albumid));
    }
}
